package org.ch04.test00.demo03;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @author CSY
 * @date 2025 - 03 - 28
 */
public class CheckField {
    private static final Class<Student> CLAZZ = Student.class;
    private static final Desc ANNOTATION = CLAZZ.getAnnotation(Desc.class);
    public static String limitSex(String sex){
        try {
            String[] limitSex = ANNOTATION.checkSex();
            boolean flag = false;
            for (String s : limitSex) {
                if(s.equals(sex)){
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                throw new Exception("性别不在范围！");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  sex;
    }

    public static String[] limitHobbies(String[] hobbies){
        try {
            String[] limitHobbies = ANNOTATION.checkHobbies();
            for (int i = 0; i < hobbies.length; i++) {
                boolean inFlag = false;
                for (int j = 0; j < limitHobbies.length; j++) {
                    if (hobbies[i].equals(limitHobbies[j])) {
                        inFlag = true;
                        break;
                    }
                }
                if(!inFlag){
                    throw new Exception(hobbies[i] + "不在指定爱好范围内："+ Arrays.toString(limitHobbies));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return hobbies;
    }
}
